package com.lyd.controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

import com.lyd.entity.FolderEntity;
import com.lyd.service.impl.FolderServiceImpl;

public class FolderController {
	static Scanner scanner = new Scanner(System.in);
	static Scanner sc = new Scanner(System.in);
	static FolderServiceImpl folderService = new FolderServiceImpl();

	public static void main(String[] args) {
		while (true) {
			System.out.println("1.添加文件夹2.修改文件夹3.删除文件夹4.查询");
			int mean = scanner.nextInt();
			if (mean == 1) {
				addFolder();
			} else if (mean == 2) {
				updateFolder();
			} else if (mean == 3) {
				deleteFolder();
			} else if (mean == 4) {
				queryFolder();
			}
		}
	}

	public static void addFolder() {
		FolderEntity folder = new FolderEntity();
		System.out.println("请输入要添加 的父文件夹parentNodeCode");
		String parentNodeCode = sc.next();
		if (folderService.isExists(parentNodeCode)) {
			System.out.println("添加的父文件夹以存在");
			return;
		}
		System.out.println("请输入父文件夹nodeName");
		String nodeName = sc.next();
		if (folderService.isExists(nodeName)) {
			System.out.println("父文件nodeName以存在，请重新输入");
		}
		System.out.println("请输入父文件夹的nodeCode");
		String nodeCode = sc.next();
		System.out.println("请输入父文件夹类型");
		String nodeType = sc.next();
		folder.setNodeType(nodeType);
		folder.setParentNodeCode(parentNodeCode);
		folder.setNodeName(nodeName);
		folder.setNodeCode(nodeCode);
		folderService.addFolder(folder);
	}

	public static void updateFolder() {
		FolderEntity folder = new FolderEntity();
		System.out.println("请输入要修改的父文件夹nodeName");
		String updateFolder = sc.next();
		if (!folderService.isExists(updateFolder)) {
			System.out.println("要修改的文件夹nodeName不存在");
			return;
		}
		System.out.println("请输入要新的nodeCode");
		String newNodeCode = sc.next();
		if (folderService.isExists(newNodeCode)) {
			System.out.println("nodeCode以存在，请重新输入");
		}
		folder.setNodeName(updateFolder);
		folder.setNodeCode(newNodeCode);
		folderService.updateFolder(folder);
	}

	public static void deleteFolder() {
		FolderEntity folder = new FolderEntity();
		System.out.println("请输入要删除的文件夹nodeName");
		String deleteFolder = sc.next();		
		if (!folderService.isExists(deleteFolder)) {
			System.out.println("要删除的文件夹nodeName不存在");
			return;
		} else {
//			folderService.deleteFolder("Y");
//			folder.setNodeName(deleteFolder);
			folderService.deleteFolder(deleteFolder);
			folder.setNodeName(deleteFolder);
		}
	}

//	public static void queryFolder() {
//		List<FolderEntity> list = folderService.listAll();
//		for (FolderEntity nodeName : list) {
//			if(nodeName.getParentNodeCode().equals("0")) {
//				System.out.println(nodeName.getNodeName());
//				queryFile(nodeName.getNodeCode(),list);
//				
//			}
//			
//		}
//	}
//	public static void queryFile(String nodeCode,List<FolderEntity> list) {
//		if(queryParentNodeCode(nodeCode,list)) {
//			for (int i = 0; i < list.size(); i++) {
//				if(list.get(i).getParentNodeCode().equals(nodeCode)) {
//					System.out.println("  "+list.get(i).getNodeName());
//					if(queryParentNodeCode(list.get(i).getNodeCode(),list)){
//						queryFile(list.get(i).getNodeCode(), list);
//					}
//				}
//			}
//		}
//		
//	}
	public static void queryFolder() {
		List<FolderEntity> list = folderService.listAll();
//		Map<String,FolderEntity> map = list.stream().
		buildFolderEntity(list);
		printNodes(list,0);
	}
	public static void printNodes(List<FolderEntity> list,int countSpace) {
		for (FolderEntity entity : list) {
			printSpace(countSpace);
			System.out.println(entity.getNodeName());
			if(entity.getChilds()!=null) {
				printNodes(entity.getChilds(),countSpace+1);
			}
		}
	}
	private static void printSpace(int countSpace) {
		while(countSpace>0) {
			System.out.print("      ");
			countSpace--;
		}
	}
	public static void queryFile(String nodeCode,List<FolderEntity> list) {
		if(queryParentNodeCode(nodeCode,list)) {
			for (int i = 0; i < list.size(); i++) {
				if(list.get(i).getParentNodeCode().equals(nodeCode)) {
					System.out.println("  "+list.get(i).getNodeName());
					if(queryParentNodeCode(list.get(i).getNodeCode(),list)){
						queryFile(list.get(i).getNodeCode(), list);
					}
				}
			}
		}
	}
	private static void buildFolderEntity(List<FolderEntity> list) {
		Map<String,FolderEntity> map = new HashMap<>();
		for(FolderEntity entity:list) {
			map.put(entity.getNodeCode(), entity);
		}
		Set<FolderEntity> nodeSet = new HashSet<>();
		for(FolderEntity entity:list) {
			//得到父节点对象
			FolderEntity value = map.get(entity.getParentNodeCode());
			//虚拟节点0判断
			if(value!=null) {
				//有没有子节点对象
				if(value.getChilds()==null) {
//					value.setChilds(new ArrayList<>());
				}
				//添加到子节点列表
				value.getChilds().add(entity);
				//要删除的节点
				nodeSet.add(entity);
			}
		}
		//删除节点
		list.removeAll(nodeSet);
//		for(Iterator<FolderEntity> iterator = list.iterator();iterator.hasNext();) {
//			FolderEntity entity = iterator.next();
//			FolderEntity value = map.get(entity.getParentNodeCode());
//			if(value!=null) {
//				if(value.getChilds()==null) {
//					value.setChilds(new ArrayList<>());
//				}
//				value.getChilds().add(entity);
//				iterator.remove();
//			}
//		}
	}
	
	public static boolean queryParentNodeCode(String nodeCode,List<FolderEntity> list) {
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i).getParentNodeCode().equals(nodeCode)) {
				return true;
			}
		}
		return false;
	}
}
